FIND_PACKAGE(Java REQUIRED)
FIND_PACKAGE(JNI REQUIRED)

#find jblas
INCLUDE(UseJava)
FIND_JAR(JBLAS NAMES jblas jblas-1.2.0
		PATHS /usr/lib/java /opt/local/share/java
			  ENV JBLAS_PATH)
IF(JBLAS)
	SET(HAVE_JBLAS 1)
ELSE()
	MESSAGE(FATAL_ERROR "JBlas could not be found!\n"
		"It is required for java interface!")
ENDIF()

SET(TARGET_SWIGFLAGS "-package\;org.shogun")

include(ShogunInterfaces)
include_directories(${JNI_INCLUDE_DIRS})

GENERATE_INTERFACE_TARGET(java ${CMAKE_CURRENT_SOURCE_DIR} "")

set(CMAKE_JAVA_TARGET_OUTPUT_NAME shogun)
SET(CMAKE_JAVA_INCLUDE_PATH ${JBLAS})

ADD_CUSTOM_COMMAND(TARGET interface_java POST_BUILD
	COMMAND cmake -E echo "Compiling Java files..."
	COMMAND cmake -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/shogun_jar
	COMMAND ${Java_JAVAC_EXECUTABLE} *.java
			-d ${CMAKE_CURRENT_BINARY_DIR}/shogun_jar
			-cp "${CMAKE_JAVA_INCLUDE_PATH}"
	COMMAND cmake -E echo "Creating jar file..."
	COMMAND ${Java_JAR_EXECUTABLE} cvf shogun.jar
			-C ${CMAKE_CURRENT_BINARY_DIR}/shogun_jar org
	WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})


#ADD_JAR(shogun_jar "${CMAKE_CURRENT_BINARY_DIR}/*.java")
#add_dependencies(shogun_jar interface_java)

IF(NOT JAVA_INSTALL_DIR)
	SET(JAVA_INSTALL_DIR "share/java")
ENDIF(NOT JAVA_INSTALL_DIR)

IF(NOT JNI_INSTALL_DIR)
	SET(JNI_INSTALL_DIR "lib/cli/shogun")
ENDIF(NOT JNI_INSTALL_DIR)

SET(INTERFACE_JAVA_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE)
INSTALL(TARGETS interface_java
	LIBRARY DESTINATION ${JNI_INSTALL_DIR}
	COMPONENT java)

SET(SHOGUN_JAR "${CMAKE_CURRENT_BINARY_DIR}/shogun.jar" PARENT_SCOPE)
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/shogun.jar
		DESTINATION ${JAVA_INSTALL_DIR}
		COMPONENT java)
